<template>
    <div class="app-container">
      <el-card class="filter-container" shadow="never">
      <div style="margin-top:15px">
        <el-button
          style="float:right"
          @click="handleAddChatter()"
          type="primary"
          icon="el-icon-edit"
          size="small">
          Add Chatter
        </el-button>
        <el-button
          style="float:right; margin-right: 15px"
          type="primary"
          @click="handleSearchList()"
          size="small">
          Search
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="Nickname:">
            <el-input style="width: 203px" v-model="listQuery.nickName" placeholder="Please enter"></el-input>
          </el-form-item>
          <el-form-item label="CID:">
            <el-input style="width: 203px" v-model="listQuery.chatterAccount" placeholder="Please enter"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
      <div class="table-container">
        <el-table ref="productCateTable"
                  style="width: 100%;"
                  :data="list"
                  v-loading="listLoading" border>
          <el-table-column label="CID" align="center">
            <template slot-scope="scope">{{scope.row.chatterAccount}}</template>
          </el-table-column>
          <el-table-column label="Nickname" width="140" align="center">
            <template slot-scope="scope">{{scope.row.nickName}}</template>
          </el-table-column>
          <el-table-column label="Binding Model" width="180" align="center">
            <div slot-scope="scope" class="model_content">{{scope.row.modeInfo}}</div>
          </el-table-column>
          <el-table-column label="Last Time Online" width="180" align="center">
            <template slot-scope="scope">{{scope.row.onlineTimeEnd}}</template>
          </el-table-column>
          <el-table-column label="Creation Time" width="180" align="center">
            <template slot-scope="scope">{{scope.row.gmtCreate }}</template>
          </el-table-column>
          <el-table-column label="Full-time/Part-time" width="180" align="center">
            <template slot-scope="scope">{{scope.row.workTypeDesc }}</template>
          </el-table-column>
          <el-table-column label="Oline State" width="160" align="center">
            <template slot-scope="scope">
              <el-tag effect="dark" :type="scope.row.online===1?'success':'info'">{{scope.row.online===1?'Online':'OffLine'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="Controls" width="320px" align="center">
            <template slot-scope="scope">
              <a 
                class="operation_item"
                @click="handleBindModel(scope.$index, scope.row)">Bind Model
              </a>
              <a
                class="operation_item"
                style="margin-left:10px; margin-right:10px;"
                @click="handlePassword(scope.$index, scope.row,0)">View Password
              </a>
               <a
                class="operation_item"
                @click="handlePassword(scope.$index, scope.row,1)">Reset Password
              </a>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
    <el-dialog
      title="Binding Model"
      :visible.sync="bindModelDialogVisible"
      width="60%"
      height="50%">
      <el-scrollbar style="height:360px;">
        <div style="margin-top: 15px">
        <el-button
          style="float:right; margin-right: 15px"
          type="primary"
          @click="handleModelSearchList()"
          size="small">
          Search
        </el-button>  
        <el-form :inline="true" :model="listModelQuery" size="small">
          <el-form-item label="Nickname:">
            <el-input style="width: 203px" v-model="listModelQuery.nickName" placeholder="Please enter"></el-input>
          </el-form-item>
          <el-form-item label="ID:">
            <el-input style="width: 203px" v-model="listQuery.userCode" placeholder="Please enter"></el-input>
          </el-form-item>
        </el-form>
      </div>
        <div class="table-container">
        <el-table ref="returnReasonTable"
                  :data="modelList"
                  style="width:100%;"
                  @selection-change="handleSelectionChange"
                  v-loading="listLoading" border>
          <el-table-column type="selection" width="60" align="center"></el-table-column>
          <el-table-column label="ID" width="180" align="center">
            <template slot-scope="scope">{{scope.row.userCode}}</template>
          </el-table-column>
          <el-table-column label="Nickname" align="center">
            <template slot-scope="scope">{{scope.row.nickName}}</template>
          </el-table-column>
        </el-table>
        </div>
        <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleModelSizeChange"
          @current-change="handleModelCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :current-page.sync="listModelQuery.pageNum"
          :page-size="listModelQuery.pageSize"
          :page-sizes="[10]"
          :total="modelTotal">
        </el-pagination>
      </div>
       </el-scrollbar>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bindModelDialogVisible = false" size="small">Cancel</el-button>
        <el-button type="primary" @click="bindModelConfirm()" size="small">Confirm</el-button>
      </span>
    </el-dialog> 
    <el-dialog
      title="Add Chatter"
      :visible.sync="chatterDialogVisible"
      width="40%">
      <el-form :model="chatterInfo"
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item :required="true" label="Nickname:">
          <el-input v-model="chatterInfo.nickName" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item :required="true" label="Full-time/Part-time:" label-width="150px">
            <el-select style="width: 250px" v-model="listQuery.workType" placeholder="Please enter" clearable>
              <el-option
                v-for="item in workTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="chatterDialogVisible = false" size="small">Cancel</el-button>
        <el-button type="primary" @click="addChatterConfirm()" size="small">Confirm</el-button>
      </span>
    </el-dialog>    
        <el-dialog
      :title="isRestPwd?'Reset Password':'View Password'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form size="small">
        <el-form-item> 
          <span>Password:</span>
          <span>{{password}}</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">Cancel</el-button>
        <el-button v-if="isRestPwd" type="primary" @click="handleDialogConfirm()" size="small">Confirm</el-button>
      </span>
        </el-dialog>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.pageSize"
          :page-sizes="[10]"
          :current-page.sync="listQuery.pageNum"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </template>
  
  <script>
    import {fetchList,deleteProductCate,updateShowStatus,updateNavStatus} from '@/api/productCate'
    import {fetchChatterList,fetchLookPassword,fetchResertPassword,fetchAddChatter,fetchBindModel} from '@/api/chatter'
    import {fetchModelList} from '@/api/model'
    export default {
      name: "productCateList",
      data() {
        return {
          list: null,
          modelList:null,
          total: null,
          modelTotal: null,
          listLoading: true,
          modelListLoading:false,
          listQuery: {
            pageNum: 1,
            pageSize: 10,
            nickName: null,
            chatterAccount: null,
            workType:null,
          },
          listModelQuery: {
            pageNum: 1,
            pageSize: 10,
            nickName: null,
            chatterAccount: null,
          },
          parentId: 0,
          isRestPwd:false,
          dialogVisible:false,
          chatterDialogVisible:false,
          bindModelDialogVisible:false,
          password:'',
          chatterInfo:{
            nickName:''
          },
          workTypeOptions: [{
          value: "1",
          label: 'Full-time'
        }, {
          value: "2",
          label: 'Part-time'
        }],
          currentRow:null,
          multipleSelection:[],
        }
      },
      created() {
        this.resetParentId();
        this.getList();
      },
      watch: {
        $route(route) {
          this.resetParentId();
          this.getList();
        }
      },
      methods: {
        resetParentId(){
          this.listQuery.pageNum = 1;
          if (this.$route.query.parentId != null) {
            this.parentId = this.$route.query.parentId;
          } else {
            this.parentId = 0;
          }
        },
        handleSelectionChange(val){
        this.multipleSelection = val;
        },
        getList() {
          this.listLoading = true;
          var params = {
            "nickName":this.listQuery.nickName === ''?null:this.listQuery.nickName,
            "chatterAccount":this.listQuery.chatterAccount===''?null:this.listQuery.chatterAccount,
            "page":this.listQuery.pageNum,
            "pageSize":this.listQuery.pageSize,
          }
          fetchChatterList(params).then(response => {
            this.listLoading = false;
            this.list = response.data.data;
            this.total = response.data.total;
          });
        },
        getModelList(){
          this.modelListLoading = true;
          const param = {
            "nickName": this.listModelQuery.nickName===''?null:this.listModelQuery.nickName,
            "modeUserCode": this.listQuery.userCode===""?null : this.listQuery.userCode,
            "hasChatterMode": false,
            "page": this.listModelQuery.pageNum,
            "pageSize":this.listModelQuery.pageSize
          };
          fetchModelList(param).then(response => {
            this.modelListLoading = false;
            this.modelList = response.data.data;
            this.modelTotal = response.data.total;
          });
        },
        handleSearchList(){
          this.listQuery.pageNum = 1;
          this.getList();
        },
        handleModelSearchList(){
          this.listQuery.pageNum = 1;
          this.getModelList();
        },
        bindModelConfirm(){
          if(this.multipleSelection==null||this.multipleSelection.length<1){
          this.$message({
            message: 'Please select the model to operate on',
            type: 'warning',
            duration: 1000
          });
          return;
         }else{
          var modeCodeList = [];
          for(let i=0;i<this.multipleSelection.length;i++){
            modeCodeList.push(this.multipleSelection[i].userCode);
          }
          let param = {
            "modeCodeList":modeCodeList,
            "chatterAccount":this.currentRow.chatterAccount,
            "nickName":this.currentRow.nickName
          }
          fetchBindModel(param).then(response=>{
              this.getList();
              this.bindModelDialogVisible = false;
              this.$message({
                message: 'Binding successful',
                type: 'success',
                duration: 1000
              });
            });
        }
        },
        addChatterConfirm(){
          if(this.chatterInfo.nickName !== '' && this.listQuery.workType !== null){
            var params = {
              "nickName":this.chatterInfo.nickName,
              "workType":parseInt(this.listQuery.workType),
            };
            fetchAddChatter(params).then(response=>{
              this.getList();
              this.chatterDialogVisible = false;
              this.$message({
                message: 'Added successfully',
                type: 'success',
                duration: 1000
              });
            })
        }else{
          this.$message({
              message: 'Required fields not filled in',
              type: 'warning',
              duration: 1000
            });
        }
        },
        handleAddChatter(){
          this.chatterDialogVisible = !this.chatterDialogVisible
        },

        handleDialogConfirm(){
          var params = {
              "chatterUserCode":this.currentRow.chatterUserCode
          }
          fetchResertPassword(params).then(response=>{
            this.dialogVisible = false;
            this.$message({
                message: 'Reset successful',
                type: 'success',
                duration: 1000
              });
          });
        },
        handlePassword(index, row,position) {
          this.currentRow = row;
          this.dialogVisible = true;
          if(position == 1){
            this.isRestPwd = true;
          }else{
            this.isRestPwd = false;
          }
            var params = {
              "chatterUserCode":row.chatterUserCode
            }
            fetchLookPassword(params).then(response=>{
                this.password = response.data.password;
            });
        },
        handleBindModel(index,row,position){
          this.currentRow = row;
          this.bindModelDialogVisible = true;
          this.getModelList();
        },
        handleDelete(index, row) {
          this.$confirm('是否要删除该品牌', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteProductCate(row.id).then(response => {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1000
              });
              this.getList();
            });
          });
        },
        handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleModelSizeChange(val){
        this.listModelQuery.pageNum = 1;
        this.listModelQuery.pageSize = val;
        this.getModelList();
      },
      handleModelCurrentChange(val){
        this.listModelQuery.pageNum = val;
        this.getModelList();
      },
      },
      filters: {
        levelFilter(value) {
          if (value === 0) {
            return '一级';
          } else if (value === 1) {
            return '二级';
          }
        },
        disableNextLevel(value) {
          if (value === 0) {
            return false;
          } else {
            return true;
          }
        }
      }
    }
  </script>
  
  <style scoped>
    .model_content{
    display: -webkit-box;
    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-line-clamp:5;
    -webkit-box-orient: vertical;
  }
  .el-scrollbar__wrap {
    overflow-x: hidden!important;
  }
  .operation_item{
    color: #409EFF; 
    border-bottom: 1px solid #409EFF;
    padding-bottom:2px;
  }
  </style>
  